<template>
  <div>
    <navigation-bar>
      <div class="searchContent">
        <el-input v-model="code" ref="code" placeholder="请刷受试者条码" size="mini" @keyup.native="examineeNameKeyup($event)"
          style="width: 120px; margin-right: 10px"></el-input>
        <el-input v-model="examineeName" placeholder="请输入姓名" size="mini"
          style="width: 120px; margin-right: 10px"></el-input>
        <el-select v-model="sex" clearable size="mini" style="width: 120px; margin-right: 10px">
          <el-option :value="item.value" :label="item.label" v-for="(item, index) in sexList"
            v-bind:key="index"></el-option>
        </el-select>
        <!-- <el-select
          v-model="status"
          size="mini"
          style="width: 120px; margin-right: 10px"
        >
          <el-option value="" label="请选择状态"></el-option>
          <el-option value="1" label="状态1"></el-option>
          <el-option value="0" label="状态2"></el-option>
        </el-select> -->
        <custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
        <custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>

        <custom-button type="custom" style="margin-right: 10px" title="宣教开始" color="#08A362" @customClick="customClick"
          icon="el-icon-video-play"></custom-button>
        <custom-button type="custom" style="margin-right: 10px" icon="el-icon-video-pause" color="#D95040" title="宣教结束"
          @customClick="customClick"></custom-button>
        <custom-button type="custom" icon="el-icon-question" color="#1684FC" title="提问和答疑"
          @customClick="customClick"></custom-button>
      </div>
    </navigation-bar>
    <table-index ref="table" :dataSource="dataSource" :tableColumns="tableColumns" :btnList="btnList" btnListWidth="130px"
      :pageSize="pageSize" :pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" @lookSMJRow="lookSMJRow"
      @viewRow="viewRow" isSizes @handleSizeChange="handleSizeChange" selection
      @handleSelectionChange="handleSelectionChange"></table-index>

    <div v-if="dialogShow">
      <custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
        <form-index :formConfig="formConfig" :type="type" ref="form">
          <!--  <div>这里还可以插入其它内容进行拓展</div> -->
        </form-index>
      </custom-dialog>
    </div>
    <div v-if="askShow">
      <custom-dialog title="提问与答疑" size="large" @dialogClose="askClose">
        <form-index :formConfig="askFormConfig" ref="askForm" @submit="ask">
          <!--  <div>这里还可以插入其它内容进行拓展</div> -->
        </form-index>
      </custom-dialog>
    </div>
    <!--    <div v-if="lmjDialogShow">-->
    <!--      <custom-dialog-->
    <!--          title="查看扫描件"-->
    <!--          size="small"-->
    <!--          @dialogClose="lmjDialogClose"-->
    <!--      >-->
    <!--        <look-scan-->
    <!--            :ImgBase64List="[-->
    <!--						require('@/assets/images/smj/smj.jpg'),-->
    <!--						require('@/assets/images/smj/smj2.jpg'),-->
    <!--					]"-->
    <!--        ></look-scan>-->
    <!--      </custom-dialog>-->
    <!--    </div>-->
  </div>
</template>

<script>
import { informedEducationForm } from "@/assets/form/managementCenter/subjectManagement/filteringPeriodManagement/informedEducationForm"
import { missionalAskForm } from "@/assets/form/managementCenter/subjectManagement/filteringPeriodManagement/missionalAskForm"
import customDialog from "@/components/customComponents/customDialog"
import tableIndex from "@/components/customComponents/customeTable/tableIndex"
import customButton from "@/components/customComponents/customForm/customButton"
import formIndex from "@/components/customComponents/customForm/formIndex"
// import lookScan from "@/components/customComponents/managementCenter/lookScan"
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import { AjaxCommon } from "@/libs/apiCommon"
import { PopupCommon } from "@/libs/popupCommon"
import { SessionCommon } from "@/libs/sessionCommon"
import { Utility } from "@/libs/utility"

export default {
  components: {
    navigationBar,
    customButton,
    tableIndex,
    customDialog,
    formIndex,
    // lookScan,
  },
  data() {
    return {
      status: "",
      state:false,
      examineeName: "",
      examineeCodes: [],
      selectItem: [],
      sex: "",
      sexList: Utility.getDictList("sex"),
      lmjDialogShow: false,
      hisCodeShow: false,
      // table 相关参数  begin
      dataSource: [],
      tableColumns: [
        {
          prop: "examineeCode",
          label: "受试者编码",
          width: 120,
        },
        {
          prop: "filterNo",
          label: "筛选号",
          width: 100,
        },
        {
          prop: "examineeName",
          label: "姓名",
          width: 120,
        },
        {
          prop: "sex",
          label: "性别",
          width: 80,
          dictName: "sex",
        },
        {
          prop: "missionalByName",
          label: "宣教人",
          width: 100,
        },
        {
          prop: "missionalBeginTime",
          label: "宣教开始时间",
          width: 170,
        },
        {
          prop: "missionalEndTime",
          label: "宣教结束时间",
          width: 170,
        },
        {
          prop: "sign",
          label: "是否签署",
          width: 100,
          dictName: "sign",
        },
        {
          prop: "signTime",
          label: "签署时间",
          width: 170,
        },
        {
          prop: "updateByName",
          label: "研究者",
        },
      ],
      btnList: ["view",
        // "lookSMJ"
      ],
      pageSize: 500,
      pageCount: 100,
      pageNo: 1,
      // table 相关参数  end

      // 弹窗是否显示
      dialogShow: false,
      code: '',
      // 弹窗标题 (新增修改不同)
      dialogTitle: "",
      // 提问和答疑的弹窗是否显示
      askShow: false,

      // form的配置信息
      formConfig: informedEducationForm,
      type: "add",

      // form的配置信息
      askFormConfig: missionalAskForm,
    }
  },

  methods: {
    examineeNameKeyup(e) {
      if (e.target.value.length > 9 && !this.state) {
        let self = this
        self.state = true
        setTimeout(() => {
          self.state = false
          self.code = ''
        }, 1000)
        console.log('------------------')
        let examineeCode = e.target.value
        let ise
        for (let i = 0; i < self.dataSource.length; i++) {
          if (self.dataSource[i].examineeCode == examineeCode) {
            this.$refs.table.handleClickTableRow(self.dataSource[i])
            ise = 1
          }
        }
        if (!ise) {
          PopupCommon.notify("error", '查无此人')
        }
      }
    },
    handleSelectionChange(d) {
      this.examineeCodes = d.length > 0 ? d.map((t) => t.examineeCode) : []
      this.selectItem = d
    },
    handleSizeChange(e) {
      this.pageSize = e
      this.initPage()
    },
    customClick(d) {
      let self = this
      if (d == "宣教开始") {
        if (this.examineeCodes.length < 1) {
          PopupCommon.notify("info", "请勾选至少一个受试者")
          return
        }
        PopupCommon.confirm("确定对已勾选的受试者进行开始宣教？")
          .then(() => {
            AjaxCommon("/ccdc/pm/em/filter/emFilterMissional/begin", "post", {
              examineeCodeList: this.examineeCodes,
            }).then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                self.resetClick()
                self.searchClick()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
          })
          .catch((e) => {
            console.log(e)
          })
      } else if (d == "宣教结束") {
        if (this.examineeCodes.length < 1) {
          PopupCommon.notify("info", "请勾选至少一个受试者")
          return
        }
        PopupCommon.confirm("确定对已勾选的受试者进行结束宣教？")
          .then(() => {
            AjaxCommon("/ccdc/pm/em/filter/emFilterMissional/end", "post", {
              examineeCodeList: this.examineeCodes,
            }).then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                self.resetClick()
                self.searchClick()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
          })
          .catch((e) => {
            console.log(e)
          })
      } else if (d == "提问和答疑") {
        if (this.examineeCodes.length !== 1) {
          PopupCommon.notify("info", "请勾选一个受试者")
          return
        }
        let formConfigItem = this.askFormConfig
        formConfigItem.map((t) => {
          Object.keys(t).map((res) => {
            if (res == "fieldName") {
              if (t[res] == "examineeCode") {
                t.filedValue = this.selectItem[0].examineeCode
              } else if (t[res] == "examineeName") {
                t.filedValue = this.selectItem[0].examineeName
              } else if (t[res] == "id") {
                t.filedValue = this.selectItem[0].id
              } else {
                t.filedValue = ""
              }
            }
          })
        })
        this.askFormConfig = formConfigItem
        this.askShow = true
      }
    },
    dialogClose() {
      this.dialogShow = false
      this.$refs.userList.handleClearTableRow()
    },
    // 提问和答疑弹窗关闭
    askClose() {
      this.askShow = false
    },
    lmjDialogClose() {
      this.lmjDialogShow = false
    },
    ask(d) {
      let self = this
      AjaxCommon("/ccdc/pm/em/filter/emFilterMissional/questionAnswer", "post", d)
        .then((res) => {
          if (res.data.code == 200) {
            PopupCommon.notify("success", "操作成功")
            self.initPage()
            self.askClose()
          } else {
            PopupCommon.notify("error", res.data.message)
          }
        })
        .catch((err) => {
          PopupCommon.notify("error", err.message)
        })
    },
    searchClick() {
      this.pageNo = 1
      this.initPage()
    },
    resetClick() {
      this.examineeName = ""
      this.sex = ""
    },
    // 分页点击事件
    pageChange(p) {
      this.pageNo = p
      this.initPage()
    },
    lookSMJRow(d) {
      console.log(d)
      console.log("暂无扫描件字段，搞张示意图片。")
      this.lmjDialogShow = true
    },
    viewRow(d) {
      this.type = "look"
      let formConfigItem = this.formConfig
      formConfigItem.map((t) => {
        Object.keys(t).map((res) => {
          if (res == "fieldName") {
            t.filedValue = d.data[t[res]]
          }
        })
      })
      this.formConfig = formConfigItem
      this.dialogShow = true
      this.dialogTitle =
        "查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    // 初始化事件
    initPage() {
      let self = this
      AjaxCommon("/ccdc/pm/em/filter/emFilterMissional/listByPage", "post", {
        examineeName: this.examineeName,
        sex: this.sex,
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.data.code == 200) {
          self.dataSource = res.data.result.records
          self.pageCount = res.data.result.total
        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },
  },
  mounted() {
    this.initPage()
    this.$refs.code.focus()
  },
}
</script>

<style></style>
